home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 309 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.1 KB  |  105 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: gnb@bby.com.au (Gregory Bond)
  3. Newsgroups: comp.std.c++
  4. Subject: proposal: renew & relocator member fn
  5. Date: 06 Feb 1996 19:25:03 PST
  6. Organization: Burdett, Buckeridge & Young Ltd., Melbourne, Australia
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <GNB.96Feb7140952@dame.bby.com.au>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 07 Feb 1996 03:09:52 GMT
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBVAwUBMRgbp0y4NqrwXLNJAQFPbwH8DLIpqRgAshMAcDv9fgjc69GfjXXOV+cG
  13.     eaczUOjR2iL8AieFLvmYWXQ25Rf/RDTA7cuYMcyRTf6vLkfCc0z08A==
  14.     =0CHE
  15. Originator: austern@isolde.mti.sgi.com
  16.  
  17. [Yes, I know it's too late to consider new features.  So consider this
  18. advance notice for the next round!]
  19.  
  20. People often ask for a "renew[]" operator to complement "new[]" in the
  21. same way realloc() complements malloc().  Of course the problem there
  22. is contructing the new objects and destructing the old ones, so a
  23. whole bunch of code gets written to do a new[], a bunch of assigns and a
  24. delete[]. This (or the moral equivalent using malloc(), placement new
  25. and delete) is especially prevalent inside container classes such
  26. as STL.
  27.  
  28. I was thinking about the efficiency concerns here.  For what is
  29. conceptually a simple operation, three calls are made - a default
  30. ctor, a copy ctor and a dtor.  The copy ctor may potentially have to
  31. allocate and deep-copy extensive data structures, so this can be very
  32. expensive, especially when shallow copy would be sufficient if the
  33. copy-ctor could KNOW that the source object was about to be destroyed.
  34.  
  35. So it seems to me that a combination of copy constructor and destructor
  36. could be used in these circumstances to "relocate" an object.  You
  37. could call this special member a "relocator".  This could offer
  38. substantial efficiency gains, especially in situations like
  39. vector<vector<A> >.
  40.  
  41. The first step would be to pick a syntax to specify the relocator.
  42. This can't be an overloaded ctor (as any ctor arg list could
  43. conceivably be a valid ctor).  One posibility is to overload the dtor:
  44.     class A {
  45.         ~A(); // destruct this
  46.         ~A(A&to);  // Contruct to and destruct this
  47.         // or perhaps ~A(void *to) as it will be passed an
  48.         // address of empty memory, not an object.  But then
  49.         // you need to cast "to" to an "A*".
  50.     } 
  51.  
  52. Another possibility is to hijack another character and use it for the
  53. relocator in the same way ~ is used for the dtor. "@" seems somewhat
  54. mnemonic for relocation but this introduces a new character into the
  55. C++ character set. A third posability is to have a special member
  56. function with an actual name (e.g. "relocator(A&)") but this is ugly.
  57.  
  58. The default relocator would be a copy ctor followed by a dtor.
  59.  
  60. One a relocator is defined, we could define renew[] (along with
  61. operator renew[]):
  62.     X *p = new[10] X;
  63.     //...
  64.     renew[20](p); // 10 relocators and 10 def ctors called
  65.     // Or, if the existing memory block is big enough, the
  66.     // relocator isn't called at all, and 10 def ctors are called.
  67.  
  68.     delete[] p;
  69.  
  70. Or, for situations such as the STL that manage raw memory via
  71. placement-new and destructors:
  72.     X*p = (X*)malloc(10 * sizeof(X));
  73.     for (int i = 0; i < 10; i++)
  74.         new(p + i) X();
  75.     //...
  76.     X* new_p = (X*)realloc(p, 20 * sizeof(X));
  77.     if (new_p == p) {
  78.         // Was extended
  79.         for (int i = 10; i < 20; i++)
  80.             new (p + i) X();
  81.     } else {
  82.         // Was moved
  83.         // Note that realloc() has done a superfluous byte copy here
  84.         for (int i = 0; i < 10; i++)
  85.             (p+i)->~X(new_p + i);// relocate from p[i] to new_p[i]
  86.         for (int i = 10; i < 20; i++)
  87.             new (p + i) X();
  88.         free(p);
  89.     }
  90.  
  91. Has this sort of thing been considered before?
  92.  
  93. Go ahead, shoot me down!
  94.  
  95. Greg.
  96. --
  97. Gregory Bond <gnb@bby.com.au> Burdett Buckeridge & Young Ltd Melbourne Australia
  98. ``Efforts to maintain the "purity" of a language only succeed in establishing an
  99.   elite class of people who know the shibboleths.  Ordinary folks know better, 
  100.   even if they don't know what "shibboleth" means.'' - Larry Wall
  101. ---
  102. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  103.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  104.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  105.